package com.shiyi.converter;

import com.shiyi.bean.User;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author wangzhanshuai
 * @date 2021/1/22 5:18 下午
 * @since 1.0
 */
@Component
public class MyConverter implements Converter<String, User> {
    @Override
    public User convert(String source) {
        if(!StringUtils.hasText(source)) {
            return null;
        }

        String[] split = source.split("-");
        if(split.length != 4) {
            return null;
        }

        User user = new User();
        user.setId(Long.parseLong(split[0]));
        user.setName(split[1]);
        user.setAge(Integer.parseInt(split[2]));
        user.setSex(split[3]);
        return user;
    }
}
